package com.marketingexpert.controller.marketingmgnt.itemrecomment;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.domain.AutoRecomment;
import com.marketingexpert.domain.User;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.marketingmgnt.MarketingService;
import com.marketingexpert.service.usermgnt.UserService;

public class GetExistAutoRecommentController implements Controller {

	private MarketingService marketingService;
	
	private Configuration configuration;
	
	private UserService userService;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		
		PrintWriter out = response.getWriter();
		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");
		try {
			
			User u = userService.queryUserByUId(parentUserId);
			
			AutoRecomment ar = marketingService.queryAutoRecommentByUserId(u.getId());
			
			if(ar != null) {
				out.print(JSONObject.fromObject(ar));
				return null;
			} else {
				return null;
			}
			
		} catch(MarketingExpertException e) {
			e.printStackTrace();
			return null;
		}
		
	}


	public MarketingService getMarketingService() {
		return marketingService;
	}


	public void setMarketingService(MarketingService marketingService) {
		this.marketingService = marketingService;
	}


	public Configuration getConfiguration() {
		return configuration;
	}


	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
